﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using System.Linq;
using PMSGSC.DAL;
using PMSGSC.BLL;
using PMSGSC.Common;

namespace PMSGSC.App
{
    public partial class frmFilterOverdueWork : DevExpress.XtraEditors.XtraForm
    {
        frmMain objMain;
        public frmFilterOverdueWork(frmMain main)
        {
            InitializeComponent();
            objMain = main;
            departmentBindingSource.DataSource = Department_BLL.GetAll(frmMain.ship.Ship_ID);
            foreach (Department d in departmentBindingSource.Cast<Department>().ToList())
            {
                comboBoxEdit1.Properties.Items.Add(d.DepartmentName);
            }
            FromDate.DateTime = DateTime.Now.Date.AddMonths(-1);
            ToDate.DateTime = DateTime.Now.Date;
        }

        private Department GetIndexDepartmentName(string name)
        {
            foreach (Department d in departmentBindingSource.Cast<Department>().ToList())
            {
                if (d.DepartmentName == name)
                    return d;
            }
            return null;
        }
        List<Department> department = new List<Department>();
        private void btnOk_Click(object sender, EventArgs e)
        {
            frmProcessBar pb = new frmProcessBar();
            System.Threading.Thread thread = new System.Threading.Thread(new System.Threading.ThreadStart(() =>
            {
                LoadSkinToForm();
                pb.ShowDialog();
            }
            ));
            thread.Start();
            bool flag = false;

            if (comboBoxEdit1.Text != "")
                department.Add(GetIndexDepartmentName(comboBoxEdit1.Text));
            else
                department = departmentBindingSource.Cast<Department>().ToList();

            flag = FilterWorkOrder(flag);

            if (department.Count > 0)
            {
                frmReviewOverDueWork overDueWork = new frmReviewOverDueWork(department);
                overDueWork.MdiParent = objMain;
                overDueWork.Show();

                if (pb.InvokeRequired)
                    pb.BeginInvoke(new Action(() => pb.Close()));

                this.Close();
            }
            else
            {
                if (pb.InvokeRequired)
                    pb.BeginInvoke(new Action(() => pb.Close()));

                XtraMessageBox.Show("Not result show display", "Information", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
            
        }

        private bool FilterWorkOrder(bool flag)
        {
            BaseDAL.DB.Refresh(System.Data.Linq.RefreshMode.OverwriteCurrentValues, BaseDAL.DB.WorkOrders);
            for (int d = 0; d < department.Count; d++)
            {
                foreach (Employee em in department[d].Employees.Cast<Employee>().ToList())
                {
                    for (int i = 0; i < em.WorkOrders.Cast<WorkOrder>().ToList().Count; i++)
                    {
                        if ((em.WorkOrders[i].NextDue == null) || (em.WorkOrders[i].NextDue < FromDate.DateTime.Date) ||
                            (em.WorkOrders[i].NextDue > ToDate.DateTime.Date) || em.WorkOrders[i].Complete == true || em.WorkOrders[i].Type == 4)
                        {
                            em.WorkOrders.RemoveAt(i);
                            i--;
                        }
                        else
                        {
                            flag = true;
                        }
                    }
                }
                if (!flag)
                {
                    department.RemoveAt(d);
                    d--;
                }
                flag = false;
            }
            return flag;
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private static void LoadSkinToForm()
        {
            DevExpress.Skins.SkinManager.EnableFormSkins();
            DevExpress.UserSkins.BonusSkins.Register();
            DevExpress.UserSkins.OfficeSkins.Register();
            DevExpress.LookAndFeel.DefaultLookAndFeel obj = new DevExpress.LookAndFeel.DefaultLookAndFeel();
            try
            {
                LookAndFeelSettings.Load("LookAndFeelSettings.dat");
            }
            catch
            {
                obj.LookAndFeel.SetSkinStyle("Blue");
            }
        }

        private void btnClear_Click(object sender, EventArgs e)
        {
            FromDate.DateTime = DateTime.Now.Date.AddMonths(-1);
            ToDate.DateTime = DateTime.Now.Date;
            comboBoxEdit1.SelectedIndex = -1;

        }
    }
}